package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.enums.*;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.dao.BProdStageInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.*;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-10
 */
@Slf4j
@Service
public class BProdStageInfoServiceImpl extends ServiceImpl<BProdStageInfoMapper, BProdStageInfo> implements IBProdStageInfoService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IBizTmpService bizTmpService;
    @Autowired
    private IBizHisService bizHisService;

    @Override
    public IPage<BProdStageInfo> findByPage(QueryRequest request, BProdStageInfo info) {
        try {
            LambdaQueryWrapper<BProdStageInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getBaseProdCode())) {
                queryWrapper.likeRight(BProdStageInfo::getBaseProdCode, info.getBaseProdCode());
            }
            if (StringUtils.isNotBlank(info.getProdCode())) {
                queryWrapper.like(BProdStageInfo::getProdCode, info.getProdCode());
            }
            if (StringUtils.isNotBlank(info.getProdType())) {
                queryWrapper.likeRight(BProdStageInfo::getProdType, info.getProdType());
            }
             if (StringUtils.isNotBlank(info.getInstId())) {
                queryWrapper.likeRight(BProdStageInfo::getInstId, info.getInstId());
            }
            Page<BProdStageInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    public OperationResult saveData(String oprMenuId, String oprMenuName, BProdStageInfo entity, UpmpUserInfoVO user, UpmpOrganization org,
                                    IUpmpPrivilegeCheckService authPrivilegeCheckService, HttpServletRequest request, String webNextUrl, String authCode)throws RedpException {
        String flag = request.getParameter("optFlag");
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        instance.add(Calendar.DAY_OF_MONTH,1);
        Date time = instance.getTime();

        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            BProdStageInfo bProdStageInfo;
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), authCode);

            String now = DateUtils.formatDate(new Date(),DateUtils.FORMAT_YYYYMMDD);
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                entity.setStageStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                entity.setFeeEffectiveDate(now);
                entity.setFeeExpiryDate(BusinessContainer.doomsday);
                entity.setCreateTime(lastUpddt);
                entity.setLastOprId(user.getId());
                entity.setLastUpdateTime(lastUpddt);
                entity.setOprId(user.getId());
                entity.setStagePrimaryKey(entity.getProdType()
                        +String.format("%2s", entity.getProdCode())+entity.getBaseProdCode()+String.format("%32s", entity.getProdStageCode())
                        +String.format("%2s", entity.getProdStageNum())+entity.getFeeEffectiveDate());
                if(ProdTypeEnum.BASE_PROD.getType().equals(entity.getProdType())){
                    bProdStageInfo = this.selectBProdStageInfoBybBaseProdInfo(entity.getProdType(), entity.getBaseProdCode(), entity.getInstId(), entity.getFeeEffectiveDate(), entity.getProdStageNum());

                }else{
                    bProdStageInfo =this.selectBProdStageInfo(entity.getProdType(),entity.getProdCode(),entity.getBaseProdCode(),entity.getInstId(),entity.getFeeEffectiveDate(),entity.getProdStageNum());
                }
                if(Common.isNotBlank(bProdStageInfo)){
                    return OperationResult.buildFailureResult("分期期数重复", result);
                }
                this.saveOrUpdate(entity);

                bizTmpService.saveBizTmp(entity, user,org.getInstId(),BizOprEnum.OPR_ADD.getType(), lastUpddt
                        , oprMenuId, oprMenuName, entity.getStageStatus(), entity.getAuditStatus(), String.valueOf(entity.getId()), webNextUrl,null,null,null);
                result.put("id", entity.getId());
                result.put("saveData", entity);
            } else {

                entity.setStageStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                entity.setFeeEffectiveDate(now);
                entity.setFeeExpiryDate(BusinessContainer.doomsday);
                entity.setCreateTime(lastUpddt);
                entity.setLastOprId(user.getId());
                entity.setLastUpdateTime(lastUpddt);
                entity.setOprId(user.getId());
                entity.setFeeEffectiveDate(DateUtils.formatDate(time,DateUtils.FORMAT_YYYYMMDD));
                entity.setStagePrimaryKey(entity.getProdType()
                        +String.format("%2s", null==entity.getProdCode()?"":entity.getProdCode())+entity.getBaseProdCode()
                        +String.format("%32s", entity.getProdStageCode())
                        +String.format("%2s", entity.getProdStageNum())+entity.getFeeEffectiveDate());
                entity.setStagePrimaryKey(entity.getProdType()
                        +String.format("%2s", entity.getProdCode())+entity.getBaseProdCode()+String.format("%32s", entity.getProdStageCode())
                        +String.format("%2s", entity.getProdStageNum())+entity.getFeeEffectiveDate());
                if(ProdTypeEnum.BASE_PROD.getType().equals(entity.getProdType())){
                    bProdStageInfo = this.selectBProdStageInfoBybBaseProdInfo(entity.getProdType(), entity.getBaseProdCode(), entity.getInstId(), entity.getFeeEffectiveDate(), entity.getProdStageNum());

                }else{
                    bProdStageInfo =this.selectBProdStageInfo(entity.getProdType(),entity.getProdCode(),entity.getBaseProdCode(),entity.getInstId(),entity.getFeeEffectiveDate(),entity.getProdStageNum());
                }
                if(Common.isNotBlank(bProdStageInfo)){
                    return OperationResult.buildFailureResult("分期期数重复", result);
                }
                this.saveOrUpdate(entity);

//                saveBizHis(entity,user,org,BizOprEnum.OPR_ADD.getType(),lastUpddt,oprMenuId,oprMenuName);
                bizHisService.saveBizHisNew(entity,entity, user,org.getInstId(),BizOprEnum.OPR_ADD.getType(), lastUpddt
                        , oprMenuId, oprMenuName, String.valueOf(entity.getId()),null,null,null);

                result.put("id", entity.getId());
                result.put("saveData", entity);
            }
        } else if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(flag)) {
            String id = request.getParameter("id");
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                return OperationResult.buildFailureResult("请求参数(id)异常!", result);
            }

            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), authCode);
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                entity.setLastOprId(user.getId());
                entity.setLastUpdateTime(lastUpddt);
                entity.setFeeExpiryDate(BusinessContainer.doomsday);
                BProdStageInfo bProdStageInfo = this.getById(id);
//                BProdStageInfo bProdStageInfoOld = bProdStageInfo;
                entity.setCreateTime(bProdStageInfo.getCreateTime());
                entity.setOprId(bProdStageInfo.getOprId());
                entity.setStageStatus(bProdStageInfo.getStageStatus());
                entity.setStagePrimaryKey(entity.getProdType()
                        +String.format("%2s", null==entity.getProdCode()?"":entity.getProdCode())+entity.getBaseProdCode()
                        +String.format("%32s", entity.getProdStageCode())
                        +String.format("%2s", entity.getProdStageNum())+entity.getFeeEffectiveDate());
/*//不更新正式表
                BeanUtils.copyProperties(entity, bProdStageInfo);
                bProdStageInfo.setStagePrimaryKey(bProdStageInfo.getProdType()
                        +String.format("%2s", null==bProdStageInfo.getProdCode()?"":bProdStageInfo.getProdCode())+bProdStageInfo.getBaseProdCode()
                        +String.format("%32s", bProdStageInfo.getProdStageCode())
                        +String.format("%2s", bProdStageInfo.getProdStageNum())+bProdStageInfo.getFeeEffectiveDate());
                this.saveOrUpdate(bProdStageInfo);
*/

                bizTmpService.saveBizTmp(bProdStageInfo, user,org.getInstId(),BizOprEnum.OPR_UPDATE.getType(), lastUpddt
                        , oprMenuId, oprMenuName, bProdStageInfo.getStageStatus(), bProdStageInfo.getAuditStatus(), String.valueOf(bProdStageInfo.getId()), webNextUrl,null,null,null);

                result.put("id", bProdStageInfo.getId());
                result.put("saveData", bProdStageInfo);
            } else {
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                entity.setLastOprId(user.getId());
                entity.setLastUpdateTime(lastUpddt);
                entity.setFeeExpiryDate(BusinessContainer.doomsday);
                BProdStageInfo bProdStageInfo = this.getById(id);
                BProdStageInfo bProdStage;
                BProdStageInfo bProdStageInfoOld = bProdStageInfo;
                entity.setCreateTime(bProdStageInfo.getCreateTime());
                entity.setOprId(bProdStageInfo.getOprId());
                entity.setStageStatus(bProdStageInfo.getStageStatus());

                BeanUtils.copyProperties(entity, bProdStageInfo);
                entity.setFeeEffectiveDate(DateUtils.formatDate(time,DateUtils.FORMAT_YYYYMMDD));
                bProdStageInfo.setStagePrimaryKey(bProdStageInfo.getProdType()
                        +String.format("%2s", bProdStageInfo.getProdCode())+bProdStageInfo.getBaseProdCode()
                        +String.format("%32s", bProdStageInfo.getProdStageCode())
                        +String.format("%2s", bProdStageInfo.getProdStageNum())+bProdStageInfo.getFeeEffectiveDate());
                entity.setStagePrimaryKey(entity.getProdType()
                        +String.format("%2s", entity.getProdCode())+entity.getBaseProdCode()+String.format("%32s", entity.getProdStageCode())
                        +String.format("%2s", entity.getProdStageNum())+entity.getFeeEffectiveDate());
                if(ProdTypeEnum.BASE_PROD.getType().equals(entity.getProdType())){
                    bProdStage = this.selectBProdStageInfoBybBaseProdInfo(entity.getProdType(), entity.getBaseProdCode(), entity.getInstId(), entity.getFeeEffectiveDate(), entity.getProdStageNum());

                }else{
                    bProdStage =this.selectBProdStageInfo(entity.getProdType(),entity.getProdCode(),entity.getBaseProdCode(),entity.getInstId(),entity.getFeeEffectiveDate(),entity.getProdStageNum());
                }
                if(Common.isNotBlank(bProdStage)&&!entity.getId().equals(bProdStage.getId())){//修改时如果查询到id不同、期数相同的数据则说明被修改数据的分期期数与库中已有数据重复了
                    return OperationResult.buildFailureResult("分期期数重复", result);
                }
                this.saveOrUpdate(bProdStageInfo);

                //历史表新增
//                saveBizHis(entity,user,org,BizOprEnum.OPR_UPDATE.getType(),lastUpddt,oprMenuId,oprMenuName);
                bizHisService.saveBizHisNew(bProdStageInfo,bProdStageInfoOld, user,org.getInstId(),BizOprEnum.OPR_UPDATE.getType(), lastUpddt
                        , oprMenuId, oprMenuName, String.valueOf(bProdStageInfo.getId()),null,null,null);
                result.put("id", bProdStageInfo.getId());
                result.put("saveData", bProdStageInfo);
            }
        } else if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {
            String id = request.getParameter("id");
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                return OperationResult.buildFailureResult("请求参数(id)异常!", result);
            }
            BProdStageInfo bProdStageInfo = this.getById(id);

            if (Common.isBlank(bProdStageInfo)) {
                log.error("根据id没有找到指定数据！");
                return OperationResult.buildFailureResult("根据id没有找到指定数据！", result);
            }
            UpmpPrivilegeCheck privilegeCheck = new UpmpPrivilegeCheck();
            String bizOpr = "";
            if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)) {
                bizOpr = BizOprEnum.OPR_STOP.getType();
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), authCode);
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                } else {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                }
            } else if (PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)) {

                bizOpr = BizOprEnum.OPR_USE.getType();
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), authCode);
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                } else {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                }
            } else if (PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {

                //注销草稿状态数据则进行物理删除
                if(BizDataStatusEnum.STATUS_DRAFT.getType().equals(bProdStageInfo.getStageStatus())){
                    this.baseMapper.deleteById(bProdStageInfo.getId());
                    //删除临时表数据
                    BizTmp tmp = bizTmpService.getById(id);
                    if (Common.isNotBlank(tmp)) {
                        bizTmpService.getBaseMapper().deleteById(tmp.getRealId());
                    }

                    return OperationResult.buildSuccessResult("数据保存处理完成", result);
                }else{
                    bizOpr = BizOprEnum.OPR_CANCEL.getType();
                    privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), authCode);
                    if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                        bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                    } else {
                        bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                    }
                }
            }
            bProdStageInfo.setLastOprId(user.getId());
//            bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
            bProdStageInfo.setLastUpdateTime(lastUpddt);
            this.saveOrUpdate(bProdStageInfo);

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                //检查临时表是否有数据，如果没用则新增
//                saveBizTmp(bProdStageInfo,user,org,bizOpr,lastUpddt,oprMenuId,oprMenuName);

                bizTmpService.saveBizTmp(bProdStageInfo, user,org.getInstId(),bizOpr, lastUpddt
                        , oprMenuId, oprMenuName, bProdStageInfo.getStageStatus(), bProdStageInfo.getAuditStatus(), String.valueOf(bProdStageInfo.getId()), webNextUrl,null,null,null);
            } else {
                //历史表新增
//                saveBizHis(entity,user,org,bizOpr,lastUpddt,oprMenuId,oprMenuName);
                bizHisService.saveBizHisNew(bProdStageInfo,bProdStageInfo, user,org.getInstId(),bizOpr, lastUpddt
                        , oprMenuId, oprMenuName, String.valueOf(bProdStageInfo.getId()),null,null,null);
            }
        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {

            String id = request.getParameter("id");
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                return OperationResult.buildFailureResult("请求参数(id)异常!", result);
            }

            //获取临时表数据并转换成实体对象
            BizTmp tmp = bizTmpService.getById(id);
            if (Common.isBlank(tmp)) {
                log.error("根据id没有找到指定临时表数据！");
                return OperationResult.buildFailureResult("根据id没有找到指定临时表数据！", result);
            }
            String bizdata = tmp.getBizData1() + tmp.getBizData2() + tmp.getBizData3();
            BProdStageInfo cupsInstInfoNew = JSON.parseObject(bizdata, BProdStageInfo.class);
            BProdStageInfo bProdStage;
            BProdStageInfo bProdStageInfo = this.getById(id);
            BProdStageInfo bProdStageInfoOld = bProdStageInfo;
            if (Common.isBlank(bProdStageInfo) && !BizOprEnum.OPR_ADD.equals(tmp.getBizOpr())) {
                log.error("根据id没有找到指定数据！");
                return OperationResult.buildFailureResult("根据id没有找到指定数据！", result);
            }

            String opt = request.getParameter("opt");
            if (Common.isBlank(opt)) {
                log.error("请求参数(opt)异常!");
                return OperationResult.buildFailureResult("请求参数(opt)异常!", result);
            }

            String bizOpr = "";
            BeanUtils.copyProperties(cupsInstInfoNew, bProdStageInfo);
            String approvalRefuseReason = request.getParameter("approvalRefuseReason");
            if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(bProdStageInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                    bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    entity.setFeeEffectiveDate(DateUtils.formatDate(time,DateUtils.FORMAT_YYYYMMDD));
                    bProdStageInfo.setStagePrimaryKey(bProdStageInfo.getProdType()
                            +String.format("%2s", bProdStageInfo.getProdCode())+bProdStageInfo.getBaseProdCode()
                            +String.format("%32s", bProdStageInfo.getProdStageCode())
                            +String.format("%2s", bProdStageInfo.getProdStageNum())+bProdStageInfo.getFeeEffectiveDate());
                    if(ProdTypeEnum.BASE_PROD.getType().equals(entity.getProdType())){
                        bProdStage = this.selectBProdStageInfoBybBaseProdInfo(bProdStageInfo.getProdType(), bProdStageInfo.getBaseProdCode(), bProdStageInfo.getInstId(), bProdStageInfo.getFeeEffectiveDate(), bProdStageInfo.getProdStageNum());

                    }else{
                        bProdStage =this.selectBProdStageInfo(bProdStageInfo.getProdType(),bProdStageInfo.getProdCode(),bProdStageInfo.getBaseProdCode(),bProdStageInfo.getInstId(),bProdStageInfo.getFeeEffectiveDate(),bProdStageInfo.getProdStageNum());
                    }
                    if(Common.isNotBlank(bProdStage)&&!id.equals(bProdStage.getId())){//新增审批通过时如果查询到id不同、期数相同的数据则说明被修改数据的分期期数与库中已有数据重复了
                        return OperationResult.buildFailureResult("分期期数重复", result);
                    }
                } else {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_ADD.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType().equals(bProdStageInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                } else {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_CANCEL.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType().equals(bProdStageInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                } else {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_STOP.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType().equals(bProdStageInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                } else {
                    bProdStageInfo.setStageStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_USE.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(bProdStageInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                    bProdStageInfo.setStageStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    entity.setFeeEffectiveDate(DateUtils.formatDate(time,DateUtils.FORMAT_YYYYMMDD));
                    bProdStageInfo.setStagePrimaryKey(bProdStageInfo.getProdType()
                            +String.format("%2s", bProdStageInfo.getProdCode())+bProdStageInfo.getBaseProdCode()
                            +String.format("%32s", bProdStageInfo.getProdStageCode())
                            +String.format("%2s", bProdStageInfo.getProdStageNum())+bProdStageInfo.getFeeEffectiveDate());
                    if(ProdTypeEnum.BASE_PROD.getType().equals(entity.getProdType())){
                        bProdStage = this.selectBProdStageInfoBybBaseProdInfo(bProdStageInfo.getProdType(), bProdStageInfo.getBaseProdCode(), bProdStageInfo.getInstId(), bProdStageInfo.getFeeEffectiveDate(), bProdStageInfo.getProdStageNum());

                    }else{
                        bProdStage =this.selectBProdStageInfo(bProdStageInfo.getProdType(),bProdStageInfo.getProdCode(),bProdStageInfo.getBaseProdCode(),bProdStageInfo.getInstId(),bProdStageInfo.getFeeEffectiveDate(),bProdStageInfo.getProdStageNum());
                    }
                    if(Common.isNotBlank(bProdStage)&&!id.equals(bProdStage.getId())){//修改审批通过时如果查询到id不同、期数相同的数据则说明被修改数据的分期期数与库中已有数据重复了
                        return OperationResult.buildFailureResult("分期期数重复", result);
                    }
                } else {
                    bProdStageInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_UPDATE.getType();
            }
            bProdStageInfo.setAuditRefuseReason(approvalRefuseReason);

            this.saveOrUpdate(bProdStageInfo);
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                //审批通过将正式表更新到历史表
                bizHisService.saveBizHisNew(bProdStageInfo,bProdStageInfoOld, user,org.getInstId(),bizOpr, lastUpddt, oprMenuId, oprMenuName, String.valueOf(bProdStageInfo.getId()),null,null,null);
/*
                ReconPathInfoR reconPathInfoR = reconPathInfoRService.findByTmpId(bProdStageInfo.getId());
                if(Common.isBlank(reconPathInfoR)){
                    reconPathInfoR = new ReconPathInfoR();
                }
                BeanUtils.copyProperties(bProdStageInfo, reconPathInfoR);
                reconPathInfoR.setTmpId(bProdStageInfo.getId());
                reconPathInfoRService.save(reconPathInfoR);
*/
            }
            // 修改临时表的审核状态等字段
            bizTmpService.saveBizTmp(bProdStageInfo, user,org.getInstId(),bizOpr, lastUpddt, oprMenuId, oprMenuName, bProdStageInfo.getStageStatus(), bProdStageInfo.getAuditStatus(), String.valueOf(bProdStageInfo.getId()), webNextUrl,null,null,null);
        }

        return OperationResult.buildSuccessResult("数据保存处理完成", result);
    }
    @Override
    public List<BProdStageInfo> getBaseProdStageList(String instId, String prodType, String baseProdCode) {

        LambdaQueryWrapper<BProdStageInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdStageInfo::getInstId, instId);
        wrapper.eq(BProdStageInfo::getProdType, prodType);
        wrapper.eq(BProdStageInfo::getBaseProdCode, baseProdCode);
        List<BProdStageInfo> list = this.list(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BProdStageInfo::getInstId, upmpSysParamService.getById(UpmpConstant.IGAS_DEFAULT_INST_ID).getPrimaryValue());
            wrapper.eq(BProdStageInfo::getProdType, prodType);
            wrapper.eq(BProdStageInfo::getBaseProdCode, baseProdCode);
            list = this.list(wrapper);
        }
        if (CollectionUtils.isNotEmpty(list)) {
            int now = Integer.valueOf(DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYYMMDD));
            List<BProdStageInfo> result = list.stream().filter(item -> Integer.valueOf(item.getFeeExpiryDate()) > now).collect(Collectors.toList());
            return result;
        }
        return list;
    }

    BProdStageInfo selectBProdStageInfo(String prodType, String prodCode, String baseProdCode, String instId, String feeEffectiveDate, Integer prodStageNum){
        LambdaQueryWrapper<BProdStageInfo> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(BProdStageInfo::getProdType,prodType);
        wrapper.eq(BProdStageInfo::getProdCode,prodCode);
        wrapper.eq(BProdStageInfo::getBaseProdCode,baseProdCode);
        wrapper.eq(BProdStageInfo::getInstId,instId);
        wrapper.eq(BProdStageInfo::getFeeEffectiveDate,feeEffectiveDate);
        wrapper.eq(BProdStageInfo::getProdStageNum,prodStageNum);
        return this.getOne(wrapper);
    }
    BProdStageInfo selectBProdStageInfoBybBaseProdInfo(String prodType, String baseProdCode, String instId, String feeEffectiveDate, Integer prodStageNum){
        LambdaQueryWrapper<BProdStageInfo> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(BProdStageInfo::getProdType,prodType);
        wrapper.eq(BProdStageInfo::getBaseProdCode,baseProdCode);
        wrapper.eq(BProdStageInfo::getInstId,instId);
        wrapper.eq(BProdStageInfo::getFeeEffectiveDate,feeEffectiveDate);
        wrapper.eq(BProdStageInfo::getProdStageNum,prodStageNum);
        return this.getOne(wrapper);
    }
    @Override
    public BProdStageInfo getfindbProdStageInfo(String instId, String prodType, String baseProdCode, String prodStageCode, Integer prodStageNum) {
        LambdaQueryWrapper<BProdStageInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdStageInfo::getInstId, instId);
        wrapper.eq(BProdStageInfo::getProdType, prodType);
        wrapper.eq(BProdStageInfo::getBaseProdCode, baseProdCode);
        wrapper.eq(BProdStageInfo::getProdStageCode, prodStageCode);
        wrapper.eq(BProdStageInfo::getProdStageNum, prodStageNum);
        return this.getOne(wrapper);
    }

    @Override
    public void delete(List<BProdStageInfo> list) {
        for (BProdStageInfo bProdStageInfo : list) {
            this.baseMapper.deleteById(bProdStageInfo.getId());
        }
    }
    @Override
    public List<BProdStageInfo> selectBaseProdStageList(String instId, String prodType, String baseProdCode){
        LambdaQueryWrapper<BProdStageInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdStageInfo::getInstId, instId);
        wrapper.eq(BProdStageInfo::getProdType, prodType);
        wrapper.eq(BProdStageInfo::getBaseProdCode, baseProdCode);
        wrapper.eq(BProdStageInfo::getStageStatus, "01");
        wrapper.orderByAsc(BProdStageInfo::getProdStageNum);
        List<BProdStageInfo> list =  this.list(wrapper);
        if(Common.isNotBlank(list)){
            int now = Integer.valueOf(DateUtils.formatDate(new Date(),DateUtils.FORMAT_YYYYMMDD));
            List<BProdStageInfo> result = list.stream().filter(item->Integer.valueOf(item.getFeeExpiryDate())>now).collect(Collectors.toList());
            return result;
        }
        return list;
    }
    @Override
    public List<BProdStageInfo> getProdStageListAdd(String instId,String prodType,String baseProdCode,String prodCode){
        LambdaQueryWrapper<BProdStageInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdStageInfo::getInstId, instId);
        wrapper.eq(BProdStageInfo::getProdType, prodType);
        wrapper.eq(BProdStageInfo::getBaseProdCode, baseProdCode);
        wrapper.eq(BProdStageInfo::getProdCode, prodCode);
        List<BProdStageInfo> list =  this.list(wrapper);
        if(Common.isNotBlank(list)){
            int now = Integer.valueOf(DateUtils.formatDate(new Date(),DateUtils.FORMAT_YYYYMMDD));
            List<BProdStageInfo> result = list.stream().filter(item->Integer.valueOf(item.getFeeExpiryDate())>now).collect(Collectors.toList());
            return result;
        }
        return list;
    }

    @Override
    public BProdStageInfo getEffectProdStageAdd(String instId, String prodType, String baseProdCode, String prodCode, String prodStageCode, Integer prodStageNum, Integer date) {
        LambdaQueryWrapper<BProdStageInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BProdStageInfo::getInstId, instId);
        wrapper.eq(BProdStageInfo::getProdType, prodType);
        wrapper.eq(BProdStageInfo::getBaseProdCode, baseProdCode);
        wrapper.eq(BProdStageInfo::getProdCode, prodCode);
        wrapper.eq(BProdStageInfo::getProdStageCode, prodStageCode);
        wrapper.eq(BProdStageInfo::getProdStageNum, prodStageNum);
        return this.getOne(wrapper);
    }
}
